package com.lzit.smart_canteen.controller;

import com.lzit.smart_canteen.domain.User;
import com.lzit.smart_canteen.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Objects;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //用户注册
    @PostMapping("/toRegister")
    public String toRegister(HttpServletRequest request, Model model) {
        String name = request.getParameter("name");
        String num = request.getParameter("num");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String password;
        if (!Objects.equals(password1, password2)) {
            model.addAttribute("result", "两次密码不一致!");
            return "/user/register";
        } else {
            password = password1;
        }
        int valid = Integer.parseInt(request.getParameter("valid"));
        User user = new User();
        user.setName(name);
        user.setNum(num);
        user.setPassword(password);
        user.setValid(valid);
        String res = userService.register(user);
        if (Objects.equals(res, "EXIST")){
            model.addAttribute("result", "该账号已存在!");
            return "/user/register";
        } else {
            return "/user/registerResult";
        }
    }
    //用户登录
    @PostMapping("/loginIn")
    public String loginIn(HttpServletRequest request, Model model) {
        User user = new User();
        String num = request.getParameter("num");
        String password = request.getParameter("password");
        user.setNum(num);
        user.setPassword(password);
        String result = userService.loginIn(user);
        if (Objects.equals(result, "NULL")) {
            model.addAttribute("result", "用户不存在!");
            return "/user/login";
        } else if (Objects.equals(result, "FALSE")) {
            model.addAttribute("result", "密码错误!");
            return "/user/login";
        } else {
            //验证通过，将该用户存入Session域，来表示已登录信息
            request.getSession().setAttribute("userInfo", userService.findByNum(num));
            return "/mobile/mobile_template";
        }
    }

    @GetMapping("/toLogout")
    public String toLogout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "/mobile/mobile_template";
    }

    //添加用户信息
    @PostMapping("/addUser")
    public String addUser(HttpServletRequest request) {
        User user = new User();
        String name = request.getParameter("name");
        String num = request.getParameter("num");
        String password = request.getParameter("password");
        int valid = Integer.parseInt(request.getParameter("valid"));
        user.setName(name);
        user.setNum(num);
        user.setPassword(password);
        user.setValid(valid);
        userService.addUser(user);
        return "index";
    }

    //查询多个用户
    //1.查找所有用户
    @GetMapping("/findAll")
    public String findAll(Model model) {
        List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "index";
    }

    //2.按姓名查找
    @GetMapping("/findByName")
    public String findByName(Model model, HttpServletRequest request) {
        List<User> userList = userService.findByName(request.getParameter("name"));
        model.addAttribute("userList", userList);
        return "index";
    }

    @GetMapping("/findById")
    public String findById(Model model, HttpServletRequest request) {
        User user = userService.findById(Integer.valueOf(request.getParameter("id")));
        model.addAttribute("user", user);
        return "index";
    }

    //3.按工号查找
//    @GetMapping("/findByNum")
//    public String findByNum(Model model) {
//        List<User> userList = userService.findByNum();
//        model.addAttribute("userList", userList);
//        return "login";
//    }
    //4.按权限查找
    @GetMapping("/findByValid")
    public String findByValid(Model model, HttpServletRequest request) {
        List<User> userList = userService.findByValid(Integer.parseInt(request.getParameter("valid")));
        model.addAttribute("userList", userList);
        return "index";
    }

}
